#include "Application.h"
#include "Constants.h"

Application::Application()
{
    appWindow = new sf::RenderWindow(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT), "The Zombie Escape");
    activeEngine = NULL;    // Ne jamais laisser ce pointeur sur NULL.
    isRunning = true;
}

void Application::launch()
{
    while(isRunning)
    {
        float time = appClock.getElapsedTime().asMilliseconds();
        if(time >= 16.66f)
        {
            appClock.restart();
            sf::Event event;
            while(appWindow->pollEvent(event))
            {
                activeEngine->onEvent(&event);
            }
            activeEngine->onTick();
        }
        appWindow->clear();
        activeEngine->onRender(appWindow);
        appWindow->display();
    }
}

void Application::setActiveMode(int mode)
{
    if(activeEngine != NULL)
        activeEngine->onClose();

    activeEngine = engines[mode];
    activeEngine->onLaunch();
}

void Application::addEngine(int mode, Engine* engine)
{
    engines[mode] = engine;
}

void Application::quit()
{
    isRunning = false;
}

Application::~Application()
{
    for(std::map<int, Engine*>::iterator it = engines.begin(); it != engines.end(); ++it)
        delete it->second;

    delete appWindow;
}
